<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="物料编码">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入物料编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!--        <el-form-item label="运单号">-->
        <!--          <el-input-->
        <!--            v-model="searchParam.trackingNumber"-->
        <!--            placeholder="请输入运单号"-->
        <!--            clearable-->
        <!--            @keyup.enter="search"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item label="供应商编码">
          <el-input
            v-model="searchParam.partnerCode"
            placeholder="请输入供应商编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="批次">
          <el-input
            v-model="searchParam.batch"
            placeholder="请输入批次"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!--        <el-form-item label="收货日期">-->
        <!--          <el-date-picker-->
        <!--            v-model="searchParam.takeTimeSearch"-->
        <!--            type="datetimerange"-->
        <!--            range-separator="至"-->
        <!--            start-placeholder="开始日期"-->
        <!--            end-placeholder="结束日期"-->
        <!--            value-format="YYYY-MM-DD HH:mm:ss"-->
        <!--            @change="getTakeTimeDate"-->
        <!--          >-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
        <el-button type="warning" plain @click="excelBoxOpen"
          ><i-ep-upload />导入</el-button
        >
        <el-dialog
          title="成品入库计划数据导入"
          v-model="excelBox"
          width="555px"
        >
          <div>
            <el-button
              @click="
                useDownload(
                  '/wms/srmasn/exportTempInside',
                  '成品入库计划上传模板',
                  searchParam
                )
              "
            >
              <template #icon>
                <i-ep-download />
              </template>
              成品入库计划上传模板下载
            </el-button>
          </div>
          <div style="margin-top: 10px">
            <input
              class="crop-input"
              type="file"
              name="image"
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              ref="onload_exl"
              @change="getfilepath"
            />
            <el-button
              @click="upload"
              :disabled="uploadBtn"
              style="cursor: pointer"
              >上传</el-button
            >
          </div>
        </el-dialog>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="订单编号"
          prop="orderNo"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <!--        <el-table-column-->
        <!--          label="送货单号"-->
        <!--          prop="asnCode"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <el-table-column
          label="供应商编号"
          prop="partnerCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="供应商名称"
          prop="partnerName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="物料编码"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="物料名称"
          prop="materialName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="批次"
          prop="batch"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <!--        <el-table-column-->
        <!--          label="供货数量"-->
        <!--          prop="quantity"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <el-table-column
          label="数量"
          prop="receivedQuantity"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="数量单位"
          prop="unitCode"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ useDictionary("MeasurementUnit", row.unitCode) }}
          </template>
        </el-table-column>
        <el-table-column
          label="入库状态"
          prop="warehousingStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="{ row }">
            <el-tag v-show="row.warehousingStatus == '1'" type="warning"
              >未入库</el-tag
            >
            <el-tag v-show="row.warehousingStatus == '2'">已入库</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="关联入库单"
          prop="correlationWarehousingOrder"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="关联出库单"
          prop="correlationOutboundOrder"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <!--        <el-table-column-->
        <!--          label="送货日期"-->
        <!--          prop="asnDate"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="预计到货日期"-->
        <!--          prop="arriveDate"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="运输方式"-->
        <!--          prop="modeTransport"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--          <template #default="{ row }">-->
        <!--            {{ useDictionary("modeTransportDic", row.modeTransport) }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="运单号"-->
        <!--          prop="trackingNumber"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="承运人"-->
        <!--          prop="carrier"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="发货联系人"-->
        <!--          prop="shippingContactPerson"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="发货联系人电话"-->
        <!--          prop="shippingContactPhone"-->
        <!--          min-width="130"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="车牌号"-->
        <!--          prop="plateNumber"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="发货状态"-->
        <!--          prop="shipmentStatus"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--          <template #default="{ row }">-->
        <!--            {{ useDictionary("ShipmentStatus", row.shipmentStatus) }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="收货状态"-->
        <!--          prop="receiveStatus"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--          <template #default="{ row }"-->
        <!--            >{{ useDictionary("ReceiveStatus", row.receiveStatus) }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="收货时间"-->
        <!--          prop="takeTime"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="收货人名"-->
        <!--          prop="takeUserName"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        >-->
        <!--        </el-table-column>-->
        <el-table-column
          label="创建方式"
          prop="addType"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ useDictionary("addType", row.addType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="扫描条码"
          prop="barCode"
          min-width="280"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="创建人名"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog2(scope.row)"
              ><i-ep-edit />打印</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <componentsEdit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />

    <labelprinting
      :visible="addDialog2.visible"
      :title="addDialog2.title"
      :data="addDialog2.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible2"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import componentsEdit from "./components/components-edit.vue";
import labelprinting from "./components/labelprinting.vue";
import { useDictStoreHook } from "@/store/modules/dictionary";

const dictionaryStore = useDictStoreHook();
defineOptions({
  name: "WMSSRMASN",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/wms/srmasn/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  orderType: "4",
  materialCode: "",
  partnerCode: "",
  batch: "",
};

const deleteParam = {
  api: "/wms/srmasn/delete",
  data: {},
};

const addDialog2 = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

/** 打开表单弹窗 */
function openDialog2(roleObj?: Object) {
  console.log(roleObj);
  addDialog2.title = "打印标签";
  getInfo2(roleObj.barCode);
}

// 查询详情
async function getInfo2(code: string) {
  request({
    url: `/wms/srmstore/barcodeVerify`,
    method: "post",
    data: { details: [{ outCode: code }] },
  })
    .then((data) => {
      addDialog2.data = data.data;
      addDialog2.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

function changeVisible2(value: boolean) {
  addDialog2.visible = value;
  console.log(value);
  if (!value) {
    addDialog2.data = {};
  }
}

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/wms/srmasn/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开ASN单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改成品入库计划";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增成品入库计划";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开ASN单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "成品入库计划详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getTakeTimeDate(data: any) {
  if (data) {
    searchParam.value.takeTimeStart = data[0];
    searchParam.value.takeTimeEnd = data[1];
  } else {
    searchParam.value.takeTimeStart = "";
    searchParam.value.takeTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/wms/srmasn/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}

// 导入excel
const uploadApi = "/wms/srmasn/uploadInside";
const excelBox = ref(false);
const uploadBtn = ref(false);
const uploadFile = ref("");
const fileName = ref("");

function excelBoxOpen() {
  excelBox.value = true;
}
function getfilepath(e: any) {
  const file = e.target.files[0];
  fileName.value = file.name;
  uploadFile.value = file;
  uploadBtn.value = false;
}

function upload() {
  if (!uploadFile.value) {
    ElMessage({
      type: "warning",
      message: "请选择文件！",
    });
    return;
  }

  let formdata = new FormData();
  formdata.append("file", uploadFile.value);
  formdata.append("orderType", "4");
  let config = { headers: { "Content-Type": "multipart/form-data" } };
  request
    .post(uploadApi, formdata, config)
    .then((res) => {
      ElMessage({
        type: "success",
        message: "上传成功！",
      });

      excelBox.value = false;
      uploadBtn.value = true;
      search();
    })
    .catch((error) => {
      console.log(error);
    });
}
</script>
